package main

import (
	"fmt"
	"github.com/golang/protobuf/proto"
	"zinx/myDemo/protobufDemo/pb"
)

func main() {
	person := &pb.Person{
		Name:   "mwh",
		Age:    20,
		Emails: []string{"1062966353@qq.com", "a1062966353@sina.com"},
		Phones: []*pb.PhoneNumber{
			&pb.PhoneNumber{
				Number: "18087474202",
				Type:   pb.PhoneType_WORK,
			},
			&pb.PhoneNumber{
				Number: "71381293",
				Type:   pb.PhoneType_HOME,
			},
		},
	}
	//编码，将person对象，也就是.proto通过协议转化得到的对象序列化
	data, err := proto.Marshal(person)
	if err != nil {
		return
	}
	//data就是我们需要进行网络传输的数据，对端需要按照Message对应的Person对象进行格式解析
	//解码
	newData := &pb.Person{}
	err = proto.Unmarshal(data, newData)
	if err != nil {
		return
	}
	fmt.Println(person)
	fmt.Println(newData)
}
